import Passport from '../utils/passport'

// eslint-disable-next-line require-await
module.exports = async (ctx, next) => {
  return Passport.authenticate('local', (err, user, info, status) => {
    console.log(user + '6...')
    if (err) {
      ctx.body = {
        code: -1,
        msg: err
      }
    } else {
      // eslint-disable-next-line no-lonely-if
      if (user) {
        ctx.body = {
          code: 0,
          msg: '登陆成功',
          user
        }
        return ctx.login(user)
      } else {
        ctx.body = {
          code: 1,
          msg: info
        }
      }
    }
  })(ctx, next)
}
